package com.shm.leetcode.concurrent;

import java.util.concurrent.Semaphore;

/**
 * @author: shm
 * @dateTime: 2020/10/18 19:37
 * @description:
 */
class FooBar1 {
    private int n;

    public FooBar1(int n) {
        this.n = n;
    }
    private Semaphore foo = new Semaphore(1);
    private Semaphore bar = new Semaphore(0);
    public void foo(Runnable printFoo) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
            foo.acquire();
        	// printFoo.run() outputs "foo". Do not change or remove this line.
        	printFoo.run();
            bar.release();
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
            bar.acquire();
            // printBar.run() outputs "bar". Do not change or remove this line.
        	printBar.run();
            foo.release();
        }
    }
}